<?php
declare (strict_types = 1);

namespace app\adminapi\controller;

use app\adminapi\business\TypeBusiness;
use app\adminapi\model\pyg_goods;
use app\BaseController;
use think\exception\ValidateException;
use think\facade\Db;
use think\Request;

class Goods extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $param = \request()->all();

        if (!is_numeric($param['limit'])){

            abort(2001,'每页显示的数量只能是数字');
    }
        if (!is_numeric($param['page'])){
            abort(2001,'页码只能是数字');
        }
        $where = [];
        if (isset($param['goods_name']) && !empty($param['goods_name'])){
            $where[] = ['goods_name','like',"%{$param['goods_name']}%"];
        }

        if (isset($param['is_on_sale']) && !empty($param['is_on_sale'] && in_array($param['is_on_sale'])))
        $data = pyg_goods::where($where)->with(['cate','brands','image'])->paginate($param['limit']);
        return success(200,'success',['data'=>$data]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        $param = $request->all();
       TypeBusiness::save_goods($param);
        dd($param);
    }

    public function login_show(){
        $data = pyg_goods::select();
        dd($data);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
